//
// Created by miaomiaole on 2023/6/5.
//

#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include "array_test.h"
#include <forward_list>
#include <iterator>
#include <stack>


MyTuple myTuple1(0, 1.4, 2, 4.6);

void print_MyTuple() {
    std::cout << get<0>(myTuple1);
    std::cout << get<1>(myTuple1);
    std::cout << get<2>(myTuple1);
    std::cout << get<3>(myTuple1) << std::endl;

    // vector 构造方法
    std::vector<item> v0;

    std::vector<item> v1{{1, "str", 4.5},
    };

    std::vector<item> v3(2, {2, "str", 45.5});
    auto allocator = v1.get_allocator();

    // 空间分配器 指定元素重复值  负责分配和释放内存的已存储分配器对象的模板参数。
    std::vector<item> v4(3, {2}, allocator);

    for (const auto &item: v4)
        std::cout << item.str << std::endl;

    std::vector<int> coll;
    for (int i = 0; i < 6; ++i)
        coll.push_back(i);

    for (int i = 0; i < coll.size(); ++i)
        std::cout << "coll[i]=" << coll[i] << std::endl;


    std::list<char> coll_str;
    for (char item = 'a'; item <= 'z'; item++)
        coll_str.push_back(item);

    for (const auto &item: coll_str) {
        std::cout << item << " ";
        std::cout << coll_str.front();
        // error 不能更改数组元素
        // coll_str.pop_front();
    }

    coll_str.pop_front();

    std::cout << "======" << coll_str.front() << "======";

    std::cout << std::endl;


    while (!coll_str.empty()) {
        std::cout << coll_str.back() << " ";

        coll_str.pop_back();
    }
    std::cout << std::endl;

    std::cout << "========================" << std::endl;


    std::forward_list<long> coll_long = {2, 4, 7, 8, 9, 2, -2, 46, -300};

    // 重新指定容器有效的元素个数,保留前两个元素
    coll_long.resize(2);
    for (const auto &item: coll_long)
        std::cout << item << std::endl;

    std::cout << "======coll_long.size()===" << sizeof(coll_long) / sizeof(long) << std::endl;
    // 不足补上 -3
    coll_long.resize(4, -3);

    for (const auto &item: coll_long)
        std::cout << item << std::endl;


}

using namespace std;

// vector 动态数组
void print_vector() {
    vector<int> v0;
    // 初始化vector 容量为3
    vector<int> v1(3);

    // 包含10个元素，初始化为元素3
    vector<int> v2(10, 3);
    for (const auto &item: v2)
        cout << "v2 = " << item << endl;

    cout << "v2.data() is " << v2.data() << endl;
    cout << " *v2.data() is " << *v2.data() << endl;


    vector<int> v3(3, -1, v1.get_allocator());
    cout << " v3 = ( ";
    for (const auto &item: v3)
        cout << item << ", ";
    vector<int> v4(3, -1, v2.get_allocator());
    for (const auto &item: v4)
        cout << "v4 " << item << " ";
    cout << endl;

    // vector 位图
    vector<bool> v_b = {1, 0, 1, 0, 1, 0, 1, true, false};
    cout << " The vector is : " << endl << "   ";
    for (const auto &item: v_b)
        cout << item << " ";
    cout << endl;

    // 反转位图
    v_b.flip();
    cout << "The flipped vector is : " << endl << "    ";
    for (const auto &item: v_b)
        cout << item << "  ";
    cout << endl;
    cout << v_b[2] << endl;
    cout << v2[2] << endl;


}

void print_deque() {
    forward_list<int> f_list{1, 2, 4, 5, 6, 103, 5, -6, 3 - 5};
    // 在第一个元素后面插入
    f_list.insert_after(f_list.begin(), {2, 4, 5, 6, 7, 8, 9});

    cout << "f_list = (";
    for (const auto &item: f_list)
        cout << item << ",";
    cout << ")" << endl;


    deque<int> c0;
    // 默认容量为3
    deque<int> c1(3);

    deque<int> c2(3, 99);


    deque<int> c3({2, 5, 67, 8, 9, 0});

    for (const auto &item: c3)
        cout << item << endl;

    for (auto i: c3) {
        cout << i << endl;
    }

}

typedef std::list<int> &ref_int_list;

void printLists(ref_int_list list1, ref_int_list list2) {
    cout << "list1:";
    copy(list1.begin(), list1.end(), ostream_iterator<int>(cout, " "));

    cout << endl;

    cout << " list2:";

    copy(list2.begin(), list2.end(), ostream_iterator<int>(cout, " "));
    cout << endl << endl;
}

void operator_list() {
    list<int> list1;
    list<int> list2;

    for (int i = 0; i < 6; ++i) {
        list1.push_back(i);
        list2.push_back(i * i);
    }
    // 编译器转型
    printLists(list1, list2);

    auto ref1 = static_cast<ref_int_list>(list1);
    auto ref2 = static_cast<ref_int_list>(list2);

    printLists(ref1, ref2);


    // 收尾交换元素
    list2.splice(list2.end(), list2, list2.begin());
    printLists(list1, list2);

    list2.sort();

    printLists(list1, list2);
    list1 = list2;
    // 去重
    list2.unique();

    printLists(list1, list2);
}


_Obj::_Obj() {
    cout << " _Obj() 无参构造" << endl;
}

_Obj::~_Obj() {
    cout << " ~_Obj() 析构函数" << endl;
}

_Obj::_Obj(const _Obj &obj) noexcept {
    this->str = obj.str;
    cout << " 深拷贝 _Obj(_Obj &obj) " << endl;
}

const string &_Obj::getStr() const {
    return str;
}

void _Obj::setStr(const string &str) {
    _Obj::str = str;
}

_Obj _Obj::operator=(_Obj obj) {
    _Obj obj1 = obj;
    cout << "operator=" << endl;
    return obj1;
}

_Obj::_Obj(const _Obj &&obj) noexcept {
    this->str = obj.str;
    cout << "移动构造" << endl;
}


void operator_number() {
    _Obj obj;
    obj.setStr("str23");

    _Obj obj_1(obj);
    _Obj obj_2 = obj_1;

    obj_1 = obj_2;

    // () 与 = 都调用拷贝构造函数
    cout << "obj addr:" << &obj << " obj_1 addr:" << &obj_1 << " " << "obj_2 addr:" << &obj_2 << " "
         << "obj: str address is  "
         << &obj.getStr() << " obj1: str address is  " << &obj_1.getStr() << endl;

    _Obj o = move(obj);

    cout << o.getStr() << endl;

}






